package utils;

import android.os.SystemClock;
import android.text.TextUtils;

import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 2018/5/8.
 */

public class TimeUtil {
    public static int getYear(){
        Calendar c = Calendar.getInstance();
        return c.get(Calendar.YEAR);
    }
    public static int getMonth(){
        Calendar c = Calendar.getInstance();
        return c.get(Calendar.MONTH);
    }
    public static int getDay(){
        Calendar c = Calendar.getInstance();
        return c.get(Calendar.DAY_OF_MONTH);
    }

    public static int getMaxDayOfMonth(){
        int month = getMonth();
        if (getYear() % 4 == 0 || getYear() % 400 == 0){  //闰年
            if (month == 2){
                return 29;
            }

            if (getDay() > 30){
                if (month == 4 || month == 6 || month == 9 || month == 11 ){
                    return 30;
                }
            }

        }else { // 平年
            if (month == 2){
                return 28;
            }

            if (getDay() > 30){
                if (month == 4 || month == 6 || month == 9 || month == 11 ){
                    return 30;
                }
            }
        }

        return getDay();
    }

    public static long getDayDate(String time) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date parse = dateFormat.parse(time);
            return parse.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static String getMillisecondDate() {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        Date date = new Date();
        String format = dateFormat.format(date);
        return format;
    }
    //获取时间,到秒
    public static String getTime(String time) {
        if (!TextUtils.isEmpty(time) && time.length() > 16) {
            String t = time.replace("T", " ");
            return t.substring(0, 16);
        } else {
            return time;
        }
    }
    //获取时间,到日
    public static String getTimeOnDate(String time) {
        String t = time.replace("T", " ");
        return t.substring(0, 10);
    }

    /**
     *  获取当前时间  接口返回的字符串
     * @param time
     * @return
     */
    public static String getNowTime(String time) {
        String t = time.replace("T", " ");
        return t.trim();
    }

    /**
     * 判断选择日期是否超过当天
     * @param chooseTime
     * @return ture 超过 false 没超过
     */
    public static boolean checkTime(String chooseTime){
        String nowTime = TimeUtil.getMillisecondDate();
        int nowTimeYear = Integer.parseInt(nowTime.substring(0, 4));
        int nowTimeMonth = Integer.parseInt(nowTime.substring(5, 7));
        int nowTimeDay = Integer.parseInt(nowTime.substring(8, 10));
        int chooseYear = Integer.parseInt(chooseTime.substring(0, 4));
        int chooseMonth = Integer.parseInt(chooseTime.substring(5, 7));
        int chooseDay = Integer.parseInt(chooseTime.substring(8, 10));

        // 选择日期不能超过当天
        if (chooseYear == nowTimeYear) {
            if (chooseMonth == nowTimeMonth) {
                if (chooseDay > nowTimeDay) {
                    return true;
                }
            } else {
                if (chooseMonth > nowTimeMonth) {
                    return true;
                }
            }
        } else {
            if (chooseYear > nowTimeYear) {

                return true;
            }
        }
        return false;
    }

   /*
 * 将时间转换为时间戳
 */
    public static String dateToStamp(String time) {
        if (TextUtils.isEmpty(time)){
            return "0";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = simpleDateFormat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long ts = date.getTime();
        return String.valueOf(ts);
    }

    /*
    * 将时间转换为时间戳戳
    */
    public static String dateToStampSeconds(String time) {
        if (TextUtils.isEmpty(time)){
            return "0";
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Date date = null;
        try {
            date = simpleDateFormat.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        long ts = date.getTime();
        return String.valueOf(ts);
    }


    static final String TAG = "SystemDateTime";

    public static void setDateTime(int year, int month, int day, int hour, int minute,int second) throws IOException, InterruptedException {

        requestPermission();

        Calendar c = Calendar.getInstance();

        c.set(Calendar.YEAR, year);
        c.set(Calendar.MONTH, month-1);
        c.set(Calendar.DAY_OF_MONTH, day);
        c.set(Calendar.HOUR_OF_DAY, hour);
        c.set(Calendar.MINUTE, minute);
        c.set(Calendar.SECOND,second);


        long when = c.getTimeInMillis();

        if (when / 1000 < Integer.MAX_VALUE) {
            SystemClock.setCurrentTimeMillis(when);
        }

        long now = Calendar.getInstance().getTimeInMillis();
        //Log.d(TAG, "set tm="+when + ", now tm="+now);

        if(now - when > 1000)
            throw new IOException("failed to set Date.");

    }

    static void requestPermission() throws InterruptedException, IOException {
        createSuProcess("chmod 666 /dev/alarm").waitFor();
    }

    static Process createSuProcess() throws IOException  {
        File rootUser = new File("/system/xbin/ru");
        if(rootUser.exists()) {
            return Runtime.getRuntime().exec(rootUser.getAbsolutePath());
        } else {
            return Runtime.getRuntime().exec("su");
        }
    }

    static Process createSuProcess(String cmd) throws IOException {

        DataOutputStream os = null;
        Process process = createSuProcess();

        try {
            os = new DataOutputStream(process.getOutputStream());
            os.writeBytes(cmd + "\n");
            os.writeBytes("exit $?\n");
        } finally {
            if(os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                }
            }
        }

        return process;
    }

    /**
     * 获取当天时间 格式：2018-01-02
     */
    public static String getShowDate(){
        if (String.valueOf(TimeUtil.getDay()).length() < 2) {
            return TimeUtil.getYear() + "-" + FormateUtils.appendZero(TimeUtil.getMonth() + 1) + "-0" + TimeUtil.getDay();
        } else {
            return TimeUtil.getYear() + "-" + FormateUtils.appendZero(TimeUtil.getMonth() + 1) + "-" + TimeUtil.getDay();
        }
    }

}
